<template>
  <div class="badRegistration">
    <DFPageList ref="dfPageListRef" :page-options="pageOptions" />
  </div>
</template>

<script lang="ts" setup name="BadRegistration">
  import lang from './lang';
  import { pageBadRegistration,exportBadRegistration } from '@/api/wip/badRegistration';
  import { useMixins } from '@/hooks/useMixins';
  import { listShutdownReason } from '@/api/base/shutdownReason';
  import { listMachine } from '@/api/base/machine';
  import { listShift } from '@/api/base/shift';
  import { getDateStr, disablePreviousDate2 } from '@/utils/dateUtil';

  const { loadLangFn, t } = useMixins();

  const dfPageListRef: any = ref(null);

  loadLangFn(lang);

  const machineList = ref([]);

  // const materialList = ref([]);

  const shiftList = ref([]);

  const shutdownReasonList = ref([]);

  const registrationTime:any = ref({
    registrationStartTime: `${getDateStr(-1)} 00:00:00`,
    registrationEndTime: `${getDateStr(0)} 23:59:59`,
    defaultValue: [
      new Date(`${getDateStr(-1)} 00:00:00`).getTime(),
      new Date(`${getDateStr(0)} 23:59:59`).getTime(),
    ],
  });

  const beforeResetFn = () => {
    registrationTime.value.registrationStartTime = `${getDateStr(-1)} 00:00:00`;
    registrationTime.value.registrationEndTime = `${getDateStr(0)} 23:59:59`;
  };

  const setRequestDataFn = (e) => {
    const { entity } = e;

    entity.registrationStartTime = new Date(registrationTime.value.registrationStartTime);

    entity.registrationEndTime = new Date(registrationTime.value.registrationEndTime);

    delete entity.registrationTime;
    // entity.exceptionFlag = 0; //异常标识 0无异常，1有异常
    return {
      ...e,
      entity,
    };
  };

  const pageOptions = {
    searchForm: {
      formSchemas: [
        {
          label: 'fastSearch',
          component: 'NInput',
          componentProps: {
            placeholder: t('badRegistration.table.plInput'),
          },
        },
        {
          label: 'shiftName',
          prop: 'shiftCode',
          component: 'NSelect',
          options: shiftList,
        },
        {
          label: 'registrationTime',
          component: 'NDatePicker',
          defaultValue: registrationTime.value.defaultValue,
          componentProps: {
            clearable: false,
            type: 'datetimerange',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            format: 'yyyy-MM-dd HH:mm:ss',
            defaultTime: ['00:00:00', '23:59:59'],
            isDateDisabled: disablePreviousDate2,
            // isTimeDisabled:(time: number) => {
            //   return getTimeDisabled(time,1);
            // },
            onUpdateValue: (_, v: string[]) => {
              console.log(_, v);
              if (v) {
                registrationTime.value.registrationStartTime = v[0];
                registrationTime.value.registrationEndTime = v[1];
              } else {
                registrationTime.value.registrationStartTime = '';
                registrationTime.value.registrationEndTime = '';
              }
            },
          },
        },
      ],
      beforeResetFn
    },
    table: {
      columns: [
        {
          label: 'badRegistrationCode',
        },
        {
          label: 'orderCode',
        },
        {
          label: 'erpNo',
        },
        {
          label: 'machineCode',
        },
        {
          label: 'machineName',
          render: (row) => {
            return h('span', {
              innerText: row.machineCode
                ? machineList.value.find((item) => item.value === row.machineCode).label
                : '-',
            });
          },
        },
        {
          label: 'materialCode',
        },
        {
          label: 'materialName',
        },
        {
          label: 'version',
        },
        {
          label: 'customName',
        },
        {
          label: 'shiftName',
        },
        {
          label: 'orderNum',
        },
        {
          label: 'remark',
          common: true,
        },
      ],
      action: {
        columns: [
          {
            label: 'edit',
            hump: true,
          },
        ],
      },
      operaColumn: [
        {
          label: 'add',
          hump: true,
        },
        {
          label:'export'
        }
      ],
      setRequestDataFn,
      requestFn: pageBadRegistration,
      exportRequestFn : exportBadRegistration
    },
  };

  async function initDictData() {
    const res = await Promise.all([listShift(), listMachine(), listShutdownReason()]);

    const [res1, res2, res3] = res;

    shiftList.value =
      res1?.data.map((item) => ({
        label: item.shiftName,
        value: item.shiftCode,
        userList: item.userList,
      })) || [];

    machineList.value =
      res2?.data.map((item) => ({ label: item.machineName, value: item.machineCode })) || [];

    shutdownReasonList.value =
      res3?.data.map((item) => ({
        label: `${item.shutdownReasonName}`,
        value: item.shutdownReasonCode,
      })) || [];
  }

  initDictData();

  // nextTick(() => {
  //   dfPageListRef.value && dfPageListRef.value.openModalFn();
  // });
</script>
